package com.example.mobileeducationtv;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class BroadcastSMSActivity extends Activity {
	
	static String tag = "Variable Passed";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reply_sms);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
		    String msg = extras.getString("message");
		    TextView tv = (TextView)findViewById(R.id.tv);
		    TextView tvnum = (TextView)findViewById(R.id.tvnum);
		    tvnum.setText("Broadcast To All");
		    tv.setText(msg);
		    Log.i(tag, "Msg is received");
		}
	}
	public void returnToActivity(View view){
	    finish();
	}
	public void onClick(View v) {
		broadcastSMS();
	}
	
	public void broadcastSMS() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			DatabaseHelper db = new DatabaseHelper(this);
			
			List<Student> allStudents = db.getAllStudents();
			
			for(Student student : allStudents) {
				
				String num = student.getNumber(); //<-- get from database and add an for loop to send to everyone
				EditText message;
			    message = (EditText)findViewById(R.id.contactMessage);
			    String sendmsg = message.getText().toString();
			    String receivemsg = extras.getString("message");
			    String msg = "Question\n" + receivemsg + "\nAnswer \n" + sendmsg; 
			    SmsManager sendSMS = SmsManager.getDefault();
				sendSMS.sendTextMessage(num, null, msg, null, null);
				
				Log.d("Messages Test", num + " \n" + msg);
			}
		}
	}
	
}
